=begin
Copyright (C) 2006  Kyle Brett <ksbrett@cc.gatech.edu>

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
=end

module Sph
  module ObjectBrowser
    # A window containing a BrowserView and
    # InspectorView.  Automatically connects
    # the two views.  Returns the BrowserWindow.
    #
    # Populate a BrowserWindow with
    # all the local variables in the current context:
    #   b = Sph::ObjectBrowser::BrowserWindow.new(binding).show
    class BrowserWindow < Gtk::Window
      # Create an empty BrowserWindow.
      #
      # Optionally, calls fill with <tt>fill_param</tt>, making possible
      # the nifty one-liner above.
      #
      # Returns the BrowserWindow
      def initialize(fill_param = nil)
        super()
        set_title("Sphereal Object Browser")
        set_size_request(800, 500)
        vpaned = Gtk::VPaned.new
        hpaned = Gtk::HPaned.new
        @ob = BrowserView.new
        @oi = InspectorView.new
        @ev = EvalView.new
        @oi.set_size_request(300, 100)
        @ob.set_size_request(500, 100)
        evalFrame = Gtk::Frame.new
        evalFrame.child = @ev
        @ob.notify.push @oi, @ev
        hpaned.pack1(Sph::scroller(@ob), true, false)
        hpaned.pack2(@oi, true, false)
        vpaned.pack1(hpaned, true, false)
        vpaned.pack2(evalFrame, true, false)
        add(vpaned)
        show_all
        fill(fill_param) unless fill_param.nil?
        self
      end

      # Add content to the browser's top level.  If <tt>param</tt>
      # is a <tt>Binding</tt>, all of the local variables
      # in the binding will be added.  <tt>param</tt>
      # can also be an object, in which case, simply,
      # the object is added.
      def fill(param, name = nil)
        if param.class != Binding
          @ob.add(param, name.nil? ? "[no-name]" : name)
        else
          eval("local_variables", param).each do |varID|
            @ob.add(eval(varID, param), varID)
          end
        end
      end
    end #class BrowserWindow
  end #module ObjectBrowser
end #module Sph